﻿#include "filename_create.h"


CFilename_Createer::CFilename_Createer()
{
	memset(m_filePath,0,60);
	memset(m_norRecDir,0,16);
	memset(m_evtRecDir,0,16);
	memset(m_captureDir,0,16);
	memset(m_thumbnailDir,0,24);
	m_chNom = 1;
	memset(m_separtor,0,8);
}

CFilename_Createer::~CFilename_Createer()
{
}

int CFilename_Createer::Init(char * rootDir,char * norDir,char * evtDir,char * captureDir,char * thumbnailDir,char * separtor,
							  int fBoxType,int chNum)
{
	int ret = 0;

	if(rootDir == NULL)
	{
		return ERR_EMPTY_POINT;
	}

	if(norDir == NULL)
	{
		return ERR_EMPTY_POINT;
	}

	if(evtDir == NULL)
	{
		return ERR_EMPTY_POINT;
	}

	if(captureDir == NULL)
	{
		return ERR_EMPTY_POINT;
	}

	strcpy(m_filePath,rootDir);
	strcpy(m_norRecDir,norDir);
	strcpy(m_evtRecDir,evtDir);
	strcpy(m_captureDir,captureDir);
	strcpy(m_thumbnailDir,thumbnailDir);
	strcpy(m_separtor,separtor);
	TRACE("Init Create OK.\n");
	m_fBoxType = fBoxType;
	m_chNom    = chNum;

	CreateRecDir();

	return ret;
}

int CFilename_Createer::GetFilePath(char * filePath,int fileType,time_t curTime,int channel)
{
	char file_name[250] = {0};
	char * pDir = NULL;

	Create_fname(file_name,fileType,curTime,channel);

	pDir = GetDir(fileType);

//  TRACE("Create file %s .\n",file_name);
#ifdef _WIN32
	sprintf(filePath,"%s%s\\%s",m_filePath,pDir,file_name);
#else
	sprintf(filePath,"%s%s/%s",m_filePath,pDir,file_name);
#endif 

	return 0;
}

int CFilename_Createer::GetPicFile(string & picName,time_t curTime,int channel)
{
	int ret = 0;
	char filePath[120] = {0};
	char time_str[32]    = {0};

	char dirDot = 0;
#ifdef _WIN32
	dirDot = '\\';
#else
	dirDot = '/';
#endif

	GetTimeStr(time_str,curTime);

	sprintf(filePath,"%s%s%c%02d_%s.jpeg",m_filePath,m_captureDir,dirDot,channel,time_str);
	
	picName = filePath;

	return ret;
}

int CFilename_Createer::Create_fname(char* fileName, int fileType,time_t curTime,int chnannel)
{
	int ret = 0;
	char time_str[32]    = {0};
	char fname_title[20] = {0};
	char fname_ext[8]    = {0};
	if(curTime  < 0)
	{
		return ERR_PARAM;
	}

	if(curTime == 0)
	{
		curTime = time(NULL);
	}

	GetTimeStr(time_str,curTime);
	GetFileNameExt(fname_ext);

	switch (fileType)
	{
	case F_NOR_REC:
	case F_NOR_REC_FORAPP:
		strcpy(fname_title,m_norRecDir);
		break;

	case F_EVT_REC:
	case F_EVT_REC_FORAPP:
		strcpy(fname_title,m_evtRecDir);
		break;

	case F_PHO_CAPTURE:
		strcpy(fname_title,m_captureDir);
		break;

	default:
		break;
	}

	//单通道 仅有一个通道时，忽略掉通道号
	if(m_chNom <= 1 && chnannel == 0)
	{
		if( 0 == strlen(m_separtor))
		{
			sprintf(fileName,"%s.%s",time_str,fname_ext);
		}
		else
		{
			sprintf(fileName,"%s_%s.%s",fname_title,time_str,fname_ext);
		}
	}
	else
	{
		sprintf(fileName,"ch%02d_%s_%s.%s",chnannel,fname_title,time_str,fname_ext);
	}

	return ret;
}

//设置正常录像目录
void CFilename_Createer::SetDir_NormalRec(char * dir)
{
	strcpy(m_norRecDir,dir);
}

//设置报警录像目录
void CFilename_Createer::SetDir_EvtRec(char * dir)
{
	strcpy(m_evtRecDir,dir);
}

//设置抓拍目录
void CFilename_Createer::SetDir_Capture(char * dir)
{
	strcpy(m_captureDir,dir);
}

char * CFilename_Createer::GetDir_NorRec()
{
	return m_norRecDir;
}


char * CFilename_Createer::GetDir_EvtRec()
{
	return m_evtRecDir;
}


char * CFilename_Createer::GetDir_Capture()
{
	return m_captureDir;
}



void CFilename_Createer::SetDir_thumbnail(char * dir)
{
	strcpy(m_thumbnailDir,dir);
}

char * CFilename_Createer::GetDir_thumbnail()
{
	return m_thumbnailDir;
}

char * CFilename_Createer:: GetDir(int fileType)
{
	switch (fileType)
	{
	case F_NOR_REC:       //通用录像文件
	case F_NOR_REC_FORAPP:      //通用录像文件方便app传输,480P
		return GetDir_NorRec();
	case F_EVT_REC:             //紧急录像文件
	case F_EVT_REC_FORAPP:      //紧急录像文件方便app传输，480P
		return GetDir_EvtRec();
	case F_PHO_CAPTURE:
		return GetDir_Capture();
	case F_THUMBNAIL_DIR:	   //录像文件缩略图目录
		return GetDir_thumbnail();
	default:
		return NULL;
	}
	return NULL;
}

void CFilename_Createer::GetPath(int fileType,string & path)
{
	int len = 0;
	if( 0 == strlen(m_filePath))
	{
		return ;
	}

	path = m_filePath;
	len = path.length();
	char dirDot = 0;
#ifdef _WIN32
	dirDot = '\\';
#else
	dirDot = '/';
#endif
	if(path[len -1] != dirDot)
	{
		path += dirDot;
	}

	path += GetDir(fileType);
	len = path.length();
	if(path[len -1] != dirDot)
	{
		path += dirDot;
	}
}

void CFilename_Createer::GetTimeStr(char * tmStr,time_t curTime)
{
	struct tm *p;

	p = localtime(&curTime);
	if(p == NULL)
	{
		return ;
	}
	sprintf(tmStr,"%04d%02d%02d_%02d%02d%02d",
		(p->tm_year+1900), (p->tm_mon + 1),
		(p->tm_mday), (p->tm_hour),
		(p->tm_min) , (p->tm_sec));

}

void CFilename_Createer::GetFileNameExt(char * fname_ext)
{
	switch (m_fBoxType)
	{
	case FBOX_AVI:
		strcpy(fname_ext,"AVI");
		break;
	case FBOX_MP4:
		strcpy(fname_ext,"MP4");
		break;
	case FBOX_ASF:
		strcpy(fname_ext,"asf");
		break;
	default:
		break;
	}
}
char * CFilename_Createer::GetDir_mount()
{
	return m_filePath;
}

int CFilename_Createer::CreateRecDir()
{
	int ret = 0;
	char destPath[80];

	sprintf(destPath,"%s%s",m_filePath,m_norRecDir);
	if(access(destPath,NULL)!=0)
	{
		CreateDir(destPath);
		TRACE("Not found. Need create dir %s.\n",destPath);
	}

	sprintf(destPath,"%s%s",m_filePath,m_evtRecDir);
	if(access(destPath,NULL)!=0)
	{
		CreateDir(destPath);
		TRACE("Not found. Need create dir %s.\n",destPath);
	}

	sprintf(destPath,"%s%s",m_filePath,m_captureDir);
	if(access(destPath, NULL)!=0)
	{
		CreateDir(destPath);
		TRACE("Not found.%s Need create dir %s.\n",m_captureDir,destPath);
	}

#ifdef _WIN32
	sprintf(destPath,"%s%s",m_filePath,m_thumbnailDir);
#else
	sprintf(destPath,"%s.%s",m_filePath,m_thumbnailDir);
#endif
	if(access(destPath, NULL)!=0)
	{
		CreateDir(destPath);
		TRACE("Not found %s. Need create dir %s.\n",m_thumbnailDir,destPath);
	}

	return ret;
}
